package cn.itsource.system.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.system.domain.SystemDictionaryType;
import cn.itsource.system.query.SystemDictionaryTypeQuery;
import cn.itsource.system.service.SystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryType")
@Api(value = "数据词典的API",description="数据词典相关的CRUD功能")
public class SystemDictionaryTypeController {

    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    //注意前端地址：http://localhost:8080/systemDictionaryType - get
    //查询所有
    @GetMapping
    @ApiOperation("查询所有")
    public List<SystemDictionaryType> findAll() {
        return systemDictionaryTypeService.findAll();
    }

    //注意前端地址：http://localhost:8080/systemDictionaryType/1 - get
    //根据id查询
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public SystemDictionaryType findOne(@PathVariable("id") Long id) {
        return systemDictionaryTypeService.findOne(id);
    }

    //注意前端地址：http://localhost:8080/systemDictionaryType/1 - delete
    //根据id删除
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryTypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }


    //注意前端地址：http://localhost:8080/systemDictionaryType - put - {}
    @PutMapping
    @ApiOperation("添加修改")
    public AjaxResult addorUpdate(@RequestBody SystemDictionaryType systemDictionaryType) {
        try {
            if (systemDictionaryType.getId() == null) {//添加操作
                systemDictionaryTypeService.add(systemDictionaryType);
            } else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            //如果成功
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    //注意前端地址：http://localhost:8080/systemDictionaryType - Patch - []
    //批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    public AjaxResult patchdel(@RequestBody Long[] ids) {
        try {
            systemDictionaryTypeService.patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "网络繁忙,请稍后重试");
        }
    }

    //分页
    @PostMapping
    @ApiOperation("分页查询")
    public PageResult<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery) {
        return systemDictionaryTypeService.queryPage(systemDictionaryTypeQuery);
    }


}
